package io.byte_stream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo03_Copy {
    public static void main(String[] args) throws IOException {

        //extracted();
        //Test();
        FileInputStream fileInputStream = new FileInputStream("添狗.txt");
        FileOutputStream fileOutputStream = new FileOutputStream("copy.txt");
        int len = fileInputStream.available();//打印文件的内存大小
        System.out.println(len);

        //开发中不推荐用这种方式
        //extracted(fileInputStream, fileOutputStream);




    }

    private static void extracted(FileInputStream fileInputStream, FileOutputStream fileOutputStream) throws IOException {
        byte[] arr = new byte[fileInputStream.available()];//创建跟文件大小一样的字节数组
        fileInputStream.read(arr);//将文件上的字节读取到内存中
        fileOutputStream.write(arr);//将字节数组中的字节数据写到文件上

        fileInputStream.close();
        fileOutputStream.close();
    }

    private static void test() throws IOException {
        FileInputStream fileInputStream = new FileInputStream("wzx.txt");
        FileOutputStream fileOutputStream = new FileOutputStream("添狗.txt");

        int num = 1000;
        while(num-- > 0) {
            fileOutputStream.write("宝，为什么我的备注不是亲爱的，而是添狗.".getBytes());
        }
    }

    private static void extracted() throws IOException {
        //创建输入流对象，关联wzx.txt
        FileInputStream fileInputStream = new FileInputStream("wzx.txt");
        //创建输出流对象，关联copy.txt
        FileOutputStream fileOutputStream = new FileOutputStream("添狗.txt");

        int read;

        while((read = fileInputStream.read()) != -1) {//不断读取每一个字节
            fileOutputStream.write(read);//将每一个字节写出
        }

        //关流
        fileInputStream.close();
        fileOutputStream.close();
    }
}
